//
//  Teacher
//
//  Created by 洋 耿 on 15/7/2.
//  Copyright (c) 2015年 ZengYuan. All rights reserved.
//
#import "AWHttpTool.h"
#import "Config.h"

@implementation AWHttpTool

+ (AFHTTPRequestOperation*)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    // 1.创建请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    mgr.responseSerializer.acceptableContentTypes = [mgr.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];


    
    // 2.发送请求
    AFHTTPRequestOperation*oper = [mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    //允许https
    oper.securityPolicy.allowInvalidCertificates = YES;

    return oper;
    
}


+ (AFHTTPRequestOperation*)post:(NSString *)url body:(NSString *)body success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    NSURL *URL = [NSURL URLWithString:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];
    
    [request setHTTPMethod:@"POST"];
    [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
 
    [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
    
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    op.responseSerializer.acceptableContentTypes = [op.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            
            NSInteger sta = [responseObject[@"status"] integerValue];
            
            if (sta == 50103) {
                NSLog(@"************** sta = %d", sta);

                [[NSNotificationCenter defaultCenter] postNotificationName:KANOTHERLOGIN_NOTI object:nil];
            }
            
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];

    [op start];

    return op;
}

+ (AFHTTPRequestOperation*)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    // 1.创建请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    mgr.responseSerializer.acceptableContentTypes = [mgr.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];    

    // 2.发送请求
    AFHTTPRequestOperation*oper = [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            
            NSInteger sta = [responseObject[@"status"] integerValue];
            NSLog(@"sta = %d", sta);
            
            success(responseObject);
        
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        if (failure) {
            failure(error);
        }
    }];
    
    //允许https
    oper.securityPolicy.allowInvalidCertificates = YES;

    return oper;

}
@end
